package com.test.mchelepesa.dialog_rice;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.mchelepesa.R;
import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseApplication_rice;
import com.test.mchelepesa.bean_rice.config_info_rice.GlobalConfig_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;

public class UpdateDialog_rice extends Dialog {

    private final boolean isForce;
    private final Runnable onCloseRunnable;

    public UpdateDialog_rice(@NonNull Context context, boolean isForce, Runnable onCloseRunnable) {
        super(context);
        this.isForce = isForce;
        this.onCloseRunnable = onCloseRunnable;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update_rice);

        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        TextView newVersionName = findViewById(R.id.dialog_update_tv_new_version_name);
        TextView descTv = findViewById(R.id.dialog_update_tv_desc);
        TextView upgradeNowTv = findViewById(R.id.dialog_update_tv_upgrade_now);
        TextView upgradeTipsTv = findViewById(R.id.dialog_update_tv_upgrade_tips);
        ImageButton closeIb = findViewById(R.id.dialog_update_ib_close);

        if (isForce){
            upgradeTipsTv.setVisibility(GONE);
            closeIb.setVisibility(GONE);
            descTv.setText(getContext().getString(R.string.upgrade_force_desc, BaseApplication_rice.gApplication_rice().getVersionName()));
        }else {
            upgradeTipsTv.setVisibility(VISIBLE);
            closeIb.setVisibility(VISIBLE);

            String desc = GlobalConfig_rice.getInstance().getLastVersionDesc();
            if (!TextUtils.isEmpty(desc)) descTv.setText(desc);
            else descTv.setText(getContext().getString(R.string.update_dialog_default_desc));

            closeIb.setOnClickListener(view -> {
                HttpManager_rice.getInstance().saveUserBuriedPoint("DIALOG_UPGRADE_CLOSE_CLICK");
                dismiss();
                if (onCloseRunnable!=null) onCloseRunnable.run();
            });
        }

        newVersionName.setText(GlobalConfig_rice.getInstance().getLastVersionName());

        upgradeNowTv.setOnClickListener(view -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("DIALOG_UPGRADE_UPGRADE_NOW_CLICK");
            getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + BaseApplication_rice.gApplication_rice().getPackageName())));
        });

    }

}
